home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / random / frmrand.frm < prev    next >
Text File  |  1995-05-02  |  8KB  |  295 lines

  1. VERSION 2.00
  2. Begin Form Random 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Random Number Generator"
  5.    ClientHeight    =   4020
  6.    ClientLeft      =   1965
  7.    ClientTop       =   1620
  8.    ClientWidth     =   4590
  9.    Height          =   4425
  10.    Left            =   1905
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   4020
  13.    ScaleWidth      =   4590
  14.    Top             =   1275
  15.    Width           =   4710
  16.    Begin TextBox txtValue 
  17.       Height          =   285
  18.       Left            =   2580
  19.       TabIndex        =   1
  20.       Top             =   660
  21.       Width           =   1095
  22.    End
  23.    Begin TextBox txtMin 
  24.       Height          =   285
  25.       Left            =   2580
  26.       TabIndex        =   2
  27.       Top             =   1860
  28.       Width           =   1095
  29.    End
  30.    Begin TextBox txtMax 
  31.       Height          =   285
  32.       Left            =   2580
  33.       TabIndex        =   3
  34.       Top             =   2460
  35.       Width           =   1095
  36.    End
  37.    Begin CommandButton cmdGenerate 
  38.       Caption         =   "&Generate"
  39.       Height          =   375
  40.       Left            =   1680
  41.       TabIndex        =   4
  42.       Top             =   3360
  43.       Width           =   1335
  44.    End
  45.    Begin Shape Shape1 
  46.       Height          =   2775
  47.       Left            =   480
  48.       Top             =   300
  49.       Width           =   3615
  50.    End
  51.    Begin Label Label1 
  52.       BackColor       =   &H00C0C0C0&
  53.       Caption         =   "Number of Values"
  54.       Height          =   375
  55.       Left            =   900
  56.       TabIndex        =   0
  57.       Top             =   660
  58.       Width           =   1335
  59.    End
  60.    Begin Label Label2 
  61.       BackColor       =   &H00C0C0C0&
  62.       Caption         =   "Minimum Value"
  63.       Height          =   375
  64.       Left            =   900
  65.       TabIndex        =   5
  66.       Top             =   1860
  67.       Width           =   1335
  68.    End
  69.    Begin Label Label3 
  70.       BackColor       =   &H00C0C0C0&
  71.       Caption         =   "Maximum Value"
  72.       Height          =   375
  73.       Left            =   900
  74.       TabIndex        =   6
  75.       Top             =   2460
  76.       Width           =   1335
  77.    End
  78. End
  79. Option Explicit
  80.     Dim sMsg As String
  81.  
  82. Sub cmdGenerate_Click ()
  83.  
  84.     'Install error handler
  85.  
  86.     On Error GoTo UnexpectedOops
  87.  
  88.     'Test for valid range
  89.  
  90.     If Val(TxtMax.Text) <= Val(TxtValue.Text) + Val(TxtMin.Text) Then
  91.         TxtMax.SetFocus
  92.         sMsg = "Range must be larger than the number of values generated."
  93.         MsgBox sMsg, 64, "Error"
  94.         sMsg = ""
  95.         Exit Sub
  96.     End If
  97.     
  98.     ReDim numbers(1 To TxtValue.Text) As Integer
  99.     Dim I As Integer, n As Integer, temp As Integer
  100.  
  101.     Randomize       ' seed random number generator
  102.  
  103.     I = 1
  104.  
  105.     Do
  106.                     ' generate random number between Min and Max
  107.         temp = Int(Rnd(1) * ((TxtMax.Text - TxtMin.Text) + 1) + TxtMin.Text)
  108.  
  109.         If I = 1 Then  ' don't test if first number (will be = to itself)
  110.             numbers(I) = temp
  111.             I = I + 1
  112.         Else
  113.             For n = 1 To I - 1
  114.                 If numbers(n) = temp Then   ' check all numbers for duplicates
  115.                     Exit For
  116.                 End If
  117.             Next n
  118.             If numbers(n) <> temp Then      ' temp is unique
  119.                 numbers(I) = temp
  120.                 I = I + 1                   ' advance counter
  121.             Else
  122.                 ' do nothing, don't save temp to numbers() and
  123.                 ' don't advance I.
  124.                 ' go through loop again to search for a unique number
  125.             End If
  126.         End If
  127.  
  128. Loop While I <= TxtValue.Text       ' repeat until you have enough unique numbers
  129.  
  130.     ' Generate message box to display numbers
  131.  
  132. For I = 1 To UBound(numbers)
  133.     sMsg = sMsg + Str$(numbers(I)) & ", "
  134. Next I
  135. MsgBox sMsg, 64, "Unique Random Numbers"
  136. sMsg = ""
  137. Exit Sub
  138.  
  139. UnexpectedOops:
  140.  
  141.     MsgBox Error$(Err)
  142.     Exit Sub
  143.  
  144.  
  145. End Sub
  146.  
  147. Sub DrawFrame (TargetControl As Control, FrameWidth, FrameStyle)
  148.  
  149.  
  150. ' Function: Draw a 3D outline around a control.
  151. ' Syntax: DrawFrame Control, Width, Style
  152. ' Control = name of control the outline should
  153. '           be drawn around
  154. ' Width   = width of the outline
  155. ' Style   = Raised or Sunken look
  156. '           0 = Raised
  157. '           1 = Sunken
  158.  
  159. ' Example: DrawFrame Text1, 2, 1
  160.  
  161. ' gives a sunken 3D look to text1
  162.  
  163.  
  164.  
  165.     
  166.     Dim lft%, Rite%, Btm%, Tp%
  167.     Dim LftLine%, BtmLine%
  168.  
  169.         'Determine style of outline
  170.     Select Case FrameStyle
  171.         Case 0                  'Raised
  172.             LftLine = 15
  173.             BtmLine = 0
  174.         Case 1                  'Sunken
  175.             LftLine = 0
  176.             BtmLine = 15
  177.     End Select
  178.     
  179.         'Calculate coordinates of outline
  180.     lft = TargetControl.Left
  181.     Rite = TargetControl.Left + TargetControl.Width
  182.     Tp = TargetControl.Top
  183.     Btm = TargetControl.Top + TargetControl.Height
  184.     TargetControl.Parent.DrawWidth = FrameWidth
  185.         
  186.         'Draw Top line
  187.     TargetControl.Parent.Line (lft, Tp)-(Rite, Tp), QBColor(LftLine)
  188.         'Draw Left line
  189.     TargetControl.Parent.Line (lft, Tp)-(lft, Btm), QBColor(LftLine)
  190.         'Draw Bottom line
  191.     TargetControl.Parent.Line (lft, Btm)-(Rite, Btm), QBColor(BtmLine)
  192.         'Draw Right Line
  193.     TargetControl.Parent.Line (Rite, Tp)-(Rite, Btm), QBColor(BtmLine)
  194.  
  195.  
  196. End Sub
  197.  
  198. Sub Form_Paint ()
  199.     
  200.     DrawFrame TxtValue, 2, 1
  201.     DrawFrame TxtMin, 2, 1
  202.     DrawFrame TxtMax, 2, 1
  203.  
  204. End Sub
  205.  
  206. Sub Form_Unload (Cancel As Integer)
  207.     About.Show
  208. End Sub
  209.  
  210. Sub txtMax_KeyPress (keyascii As Integer)
  211.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  212.         keyascii = 0            ' cancel the character
  213.         Beep                    ' sound error signal
  214.     End If
  215. End Sub
  216.  
  217. Sub txtMax_LostFocus ()
  218.     If TxtMax.Text = "" Then
  219.         TxtMax.SetFocus
  220.         sMsg = "Please enter a Maximum value."
  221.         MsgBox sMsg, 64, "Error"
  222.         sMsg = ""
  223.         Exit Sub
  224.     End If
  225.     
  226.     If Val(TxtMax.Text) <= Val(TxtMin.Text) Then
  227.         TxtMax.SetFocus
  228.         sMsg = "Maximum value must be greater than minimum value."
  229.         MsgBox sMsg, 64, "Error"
  230.         sMsg = ""
  231.         Exit Sub
  232.     End If
  233.  
  234.     If Val(TxtMax.Text) <= Val(TxtValue.Text) + Val(TxtMin.Text) Then
  235.         TxtMax.SetFocus
  236.         sMsg = "Range must be larger than the number of values generated."
  237.         MsgBox sMsg, 64, "Error"
  238.         sMsg = ""
  239.         Exit Sub
  240.     End If
  241.  
  242.     If Val(TxtMax.Text) >= 32767 Then
  243.         sMsg = "Number must be less than 32,767."
  244.         Beep
  245.         MsgBox sMsg, 64, "Error"
  246.         TxtMax.SetFocus
  247.         sMsg = ""
  248.         Exit Sub
  249.     End If
  250. End Sub
  251.  
  252. Sub txtMin_KeyPress (keyascii As Integer)
  253.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  254.         keyascii = 0            ' cancel the character
  255.         Beep                    ' sound error signal
  256.     End If
  257. End Sub
  258.  
  259. Sub txtMin_LostFocus ()
  260.     If TxtMin.Text = "" Then
  261.         TxtMin.SetFocus
  262.         sMsg = "Please enter a Minimum value."
  263.         MsgBox sMsg, 64, "Error"
  264.         sMsg = ""
  265.         Exit Sub
  266.     End If
  267.  
  268.     If Val(TxtMin.Text) >= 32767 Then
  269.         sMsg = "Number must be less than 32,767."
  270.         Beep
  271.         MsgBox sMsg, 64, "Error"
  272.         TxtMin.SetFocus
  273.         sMsg = ""
  274.         Exit Sub
  275.     End If
  276. End Sub
  277.  
  278. Sub txtValue_KeyPress (keyascii As Integer)
  279.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  280.         keyascii = 0            ' cancel the character
  281.         Beep                    ' sound error signal
  282.     End If
  283. End Sub
  284.  
  285. Sub txtValue_LostFocus ()
  286.     If TxtValue.Text = "" Then
  287.         TxtValue.SetFocus
  288.         sMsg = "Please enter a number of values to generate."
  289.         MsgBox sMsg, 64, "Error"
  290.         sMsg = ""
  291.         Exit Sub
  292.     End If
  293. End Sub
  294.  
  295.